<!DOCTYPE html>
<html>

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no" />
    <title>Simple Map</title>
    <link rel="stylesheet" href="https://js.arcgis.com/3.25/esri/css/esri.css">
    <style>
        html,
        body,
        #map {
            height: 100%;
            margin: 0;
            padding: 0;
        }
        
        input {
            position: absolute;
            top: 20px;
            z-index: 999;
            height: 30px;
            width: 80px;
            cursor: pointer
        }
        
        #drawbtn {
            left: 60px;
        }
        
        #stopbtn {
            left: 150px;
        }
        
        #test {
            left: 250px;
        }
        
        #ok {
            left: 400px;
        }
    </style>
    <script src="https://js.arcgis.com/3.25/"></script>
    <script src="js/jquery-2.0.3.min.js"></script>
    <script src="js/query.js"></script>
    <script>
        require([
            "esri/map",
            "esri/layers/ArcGISDynamicMapServiceLayer",
            //"esri/layers/ArcGISTiledMapServiceLayer",
            "esri/toolbars/draw",
            //"esri/layers/MapImage",
            "esri/tasks/IdentifyTask",
            "esri/tasks/QueryTask",
            "esri/tasks/query",
            "esri/layers/GraphicsLayer",
            "esri/tasks/IdentifyParameters",
            "esri/geometry/geometryEngine",
            "dojo/domReady!"
        ], function(
            Map,
            ArcGISDynamicMapServiceLayer,
            //ArcGISTiledMapServiceLayer,
            Draw,
            //MapImage,
            IdentifyTask,
            QueryTask,
            Query,
            GraphicsLayer,
            IdentifyParameters,
            geometryEngine
        ) {
            var map = new Map("map", {
                basemap: "osm",
                center: [119.4, 25.6],
                zoom: 11
            });
            var toolAreaGraphicLayer = new GraphicsLayer();
            map.addLayer(toolAreaGraphicLayer);
            var layer = new esri.layers.ArcGISDynamicMapServiceLayer("http://192.168.1.166:6080/arcgis/rest/services/MyMapService/MapServer");
            map.addLayer(layer);
            var toolbar = new esri.toolbars.Draw(map);
            //画图开启
            $("#drawbtn").click(function() {
                toolbar.activate(esri.toolbars.Draw.POLYGON);
                dojo.connect(toolbar, "onDrawEnd", function(geometry) {
                    var symbol = new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_SOLID, new dojo.Color([0, 0, 255]), 2);
                    var areafill = new esri.symbol.SimpleFillSymbol(esri.symbol.SimpleFillSymbol.STYLE_SOLID, symbol, new dojo.Color([128, 128, 128]), 2);
                    var graphic = new esri.Graphic(geometry, areafill);
                    toolAreaGraphicLayer.clear();
                    toolAreaGraphicLayer.add(graphic);
                    map.addLayer(toolAreaGraphicLayer);
                    identifyQuery(geometry);
                });

                function identifyQuery(geometry) {
                    var identify = new esri.tasks.IdentifyTask("http://192.168.1.166:6080/arcgis/rest/services/MyMapService/MapServer");
                    var params = new IdentifyParameters(); //查询参数
                    params.geometry = geometry;
                    params.returnGeometry = true;
                    params.layerIds = [2];
                    params.mapExtent = map.extent;
                    params.tolerance = 3;
                    identify.execute(params, function(res) {
                        console.log(res.length);
                        var geo = res[0].feature.geometry;
                        console.log(geo);
                        if (res.length = 1) {
                            var dysf = res[0].value;
                            console.log(dysf);
                            var areaparameter = geometryEngine.planarArea(geo, "square-meters").toFixed(5);
                            queryparame(dysf, areaparameter);
                        };
                    });
                };

                function queryparame(dysf, areaparameter) {
                    console.log(dysf);
                    console.log(areaparameter);
                    var queryTask = new QueryTask("http://localhost:6080/arcgis/rest/services/MyMapService/MapServer/2");
                    var query = new Query();
                    query.where = "基本单元码=\'" + dysf + "\'";
                    //返回所有字段
                    query.outFields = ["*"];
                    //返回几何信息。
                    query.returnGeometry = true;
                    queryTask.execute(query, function(results) {


                        var sbrjlMax = 3.5;
                        var sbrjlMin = 3.2;
                        var ykfydmj = results.features[0].attributes["已开发用地面积（公顷）"];
                        var v = results.features[0].attributes;
                        var v = results.features[0].attributes.最小开发量;
                        //已开发用地面积（公顷）
                        var ykfydmj = results.features[0].attributes["已开发用地面积（公顷）"];
                        //未开用地面积（公顷）
                        var wkfydmj = results.features[0].attributes["未开发用地面积（公顷）"];
                        //城中村面积（公顷）
                        var czcmj = results.features[0].attributes["城中村面积（公顷）"];
                        //现状开发量（㎡）
                        var xzkfl = results.features[0].attributes["现状开发量（平方米）"];
                        //剩余开发量（㎡）
                        var sykfl = results.features[0].attributes["剩余开发量（平方米）"];
                        //最大开发量
                        var zdkfl = results.features[0].attributes["最大开发量（平方米）"];

                        //最小开发量
                        var zxkfl = results.features[0].attributes["最小开发量（平方米）"];

                        //容积率上限
                        var maxZiduan = results.features[0].attributes["容积率上限"];
                        //容积率下限
                        var minZiduan = results.features[0].attributes["容积率下限"];
                        //已开发量=现状开发量（平方米）
                        var ykfl = results.features[0].attributes["现状开发量（平方米）"];
                        //剩余用地面积
                        var syydmj = results.features[0].attributes["剩余开发量（平方米）"];
                        //地块开发后剩余居住用地规划容积率上限：（最大开发量-已开发量）/剩余用地面积
                        var rongjiMax = (zdkfl - ykfl - areaparameter) / (syydmj - areaparameter);
                        //地块开发后剩余居住用地规划容积率下限：（最小开发量-已开发量）/剩余用地面积
                        var rongjiMin = (zxkfl - ykfl - areaparameter) / (syydmj - areaparameter);


                        debugger;
                        console.log(rongjiMax);
                        console.log("-----------");
                        console.log(rongjiMin);

                        //判断生成报告
                        if (sbrjlMax <= maxZiduan && rongjiMax >= minZiduan && rongjiMin <= maxZiduan && sbrjlMin >= minZiduan) {
                            //第一条建议
                            var advise1 = "商服用地申报容积率上、下限符合单元控规总量要求。";
                            var getadvise = advise1;
                        } else if (sbrjlMax >= maxZiduan && rongjiMax >= minZiduan || sbrjlMin <= minZiduan && rongjiMin <= maxZiduan) {
                            //第二条建议
                            var advise2 = "其它可兼容用地申报容积率上、下限符合单元控规总量要求。";
                            var getadvise = advise2;
                        } else if (sbrjlMax >= maxZiduan && rongjiMax <= minZiduan || sbrjlMin <= minZiduan && rongjiMin >= maxZiduan) {
                            //第三条建议
                            var advise3 = "其它不兼容用地申报容积率上、下限符合单元控规总量要求。";
                            var getadvise = advise3;
                        } else if (sbrjlMax <= maxZiduan && sbrjlMax >= minZiduan && sbrjlMin <= maxZiduan && sbrjlMin >= minZiduan) {
                            //第四条建议
                            var advise4 = "商服用地申报容积率上限高于单元同类用地容积率上限，但考虑到本地块开发后剩余同类用地开发量还可在本单元内平衡，保持总量不变。为此，建议启动地块控规单元控规动态维护工作，并报局务会议批准后实施。";
                            var getadvise = advise4;
                        } else if (sbrjlMax >= maxZiduan && rongjiMax >= minZiduan || sbrjlMin <= minZiduan && rongjiMin <= maxZiduan) {
                            //第五条建议
                            var advise5 = "其它可兼容用地申报容积率上限高于单元同类用地容积率上限，但考虑到本地块开发后剩余同类用地开发量还可在本单元内平衡，保持总量不变。为此，建议启动地块控规单元控规动态维护工作，并报局务会议批准后实施。";
                            var getadvise = advise5;
                        } else if (sbrjlMax >= maxZiduan && rongjiMax <= minZiduan || sbrjlMin <= minZiduan && rongjiMin >= maxZiduan) {
                            //第六条建议
                            var advise6 = "其它不兼容用地申报容积率上限高于单元同类用地容积率上限，但考虑到本地块开发后剩余同类用地开发量还可在本单元内平衡，保持总量不变。为此，建议启动地块控规单元控规动态维护工作，并报局务会议批准后实施。";
                            var getadvise = advise6;
                        } else if (sbrjlMax <= maxZiduan && rongjiMax >= minZiduan && sbrjlMin <= maxZiduan && sbrjlMin >= minZiduan) {
                            //第七条建议
                            var advise7 = "鉴于本地块商服用地开发后剩余同类用地开发量无法在本单元内进行平衡，为此建议启动地块控规单元修编工作，并报原审批机关批准后实施。";
                            var getadvise = advise7;
                        } else if (sbrjlMax >= maxZiduan && rongjiMax >= minZiduan || sbrjlMin <= minZiduan && rongjiMin <= maxZiduan) {
                            //第八条建议
                            var advise8 = "鉴于本地块其它可兼容用地开发后剩余同类用地开发量无法在本单元内进行平衡，为此建议启动地块控规单元修编工作，并报原审批机关批准后实施。";
                            var getadvise = advise8;
                        } else if (sbrjlMax >= maxZiduan && rongjiMax <= minZiduan || sbrjlMin <= minZiduan && rongjiMin >= manZiduan) {
                            //第九条建议
                            var advise9 = "鉴于本地块其它可兼容用地开发后剩余同类用地开发量无法在本单元内进行平衡，为此建议启动地块控规单元修编工作，并报原审批机关批准后实施。";
                            var getadvise = advise9;
                        }
                        console.log(getadvise);
                        //定义一个返回（商服地块开发强度评估报告）参数的对象
                        var sfdkobj = {
                            rongjiMax: rongjiMax,
                            rongjiMin: rongjiMin,
                            advises: getadvise
                        };
                        console.log(sfdkobj);
                        return sfdkobj;
                    });
                };
            });
        });
    </script>
</head>

<body>
    <div id="map">
        <input id="drawbtn" type="button" value="画图" />
        <input id="stopbtn" type="button" value="停止" />
        <input id="test" name="test" type="text" />
        <input id="ok" type="button" value="确定">
    </div>
</body>

</html>